iOS Programming E7:C1 快速上手构建 Quiz App

创建视图

  1. 在 Main.storyboard 文件中,从 Library 中拖拽控件到 Canvas 上,类似 Android XML,可以在 Document outline 中编辑 View 的属性
  2. 使用 Auto Layout 来保证页面中 View 的布局正确。Auto Layout 为页面中每个 View 制定位置和大小约束,这些约束可以相对于相邻 View 或者父容器 View。(类似于 Android 中的约束布局)

Auto Layout 使用初体验:

image.png

连接代码和 Interface Builder(Storyboard)

class ViewController: UIViewController {

    @IBOutlet var questionLabel: UILabel!  // 申明一个 View 的 Outlet,还要在 Interface Builder 中手动绑定
    @IBOutlet var answerLabel: UILabel!
    
    @IBAction func showNextQuestion(_ sender: UIButton) {  // 申明一个 View 的 Action,也要在 Interface Builder 中绑定
    }

    @IBAction func showAnswer(_ sender: UIButton) {
        
    }

}

总结:outlets 类似 Android 中的 findViewById,Action 类似 Android 中 Button 在 XML 中设置 android:onClick="showAnswer" , 关联到某个函数上。总体上不如 Android 写的方便。

实现完整业务逻辑:

class ViewController: UIViewController {

    @IBOutlet var questionLabel: UILabel!
    @IBOutlet var answerLabel: UILabel!
    
    let questions: [String] = [
        "What is 7+7?",
        "What is the capital of Vermont?",
        "What is cognac made from?"
    ]
    
    let answers: [String] = [
        "14",
        "Montpelier",
        "Grapes"
    ]
    
    var currentQuestionIndex: Int = 0
    
    @IBAction func showNextQuestion(_ sender: UIButton) {
        currentQuestionIndex += 1
        if currentQuestionIndex == questions.count {
            currentQuestionIndex = 0
        }
        let question: String = questions[currentQuestionIndex]
        questionLabel.text = question
        answerLabel.text = "???"
    }

    @IBAction func showAnswer(_ sender: UIButton) {
        let answer: String = answers[currentQuestionIndex]
        answerLabel.text = answer
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        questionLabel.text = questions[currentQuestionIndex]
    }
}

编译时遇到了 Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) 错误,原因是一开始在代码中申明 Outlets,进行了绑定,后面发现变量名写错了,改了变量名,在 Interface Builder 中重新进行了绑定,但之前的变量名在 InterfaceBuilder 中还存在,需要把它删掉才行。真离谱啊。

给 App 设置 App Icon,设置 Launch Screen。

运行到真机上:
需要一个 Apple Developer Program account 才能在设备上运行 App 并将 App 提交到 App Store。
免费账号:可以在设备上运行 App,但不能提交 App 到 App Store
付费账号:可以提交 App 到 App Store